<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>各类Nginx配置</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  </style>
</head>
<body>
<h3 id="一高级代理代理转发和负载均衡">一、高级代理代理转发和负载均衡</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" title="1"><span class="ex">http</span> {</a>
<a class="sourceLine" id="cb1-2" title="2">    <span class="co"># 限制器配置：基于客户端IP限制请求速率</span></a>
<a class="sourceLine" id="cb1-3" title="3">    <span class="ex">limit_req_zone</span> <span class="va">$binary_remote_addr</span> zone=healthcheck:10m rate=10r/s<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-4" title="4">    <span class="co"># $binary_remote_addr：使用客户端 IP 的二进制形式作为标识键</span></a>
<a class="sourceLine" id="cb1-5" title="5">    <span class="co"># zone=healthcheck:10m：创建名为healthcheck的共享内存区，大小 10MB（约存储 50 万 IP 状态）</span></a>
<a class="sourceLine" id="cb1-6" title="6">    <span class="co"># rate=10r/s：限制每个 IP 每秒最多 10 个请求（突发时可处理 10 个，超出则返回 503）</span></a>
<a class="sourceLine" id="cb1-7" title="7"></a>
<a class="sourceLine" id="cb1-8" title="8">    <span class="co"># 负载均衡组</span></a>
<a class="sourceLine" id="cb1-9" title="9">    <span class="ex">upstream</span> backend_servers {</a>
<a class="sourceLine" id="cb1-10" title="10">        <span class="ex">least_conn</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-11" title="11">        <span class="ex">ip_hash</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-12" title="12">        <span class="co"># 负载均衡策略：</span></a>
<a class="sourceLine" id="cb1-13" title="13"></a>
<a class="sourceLine" id="cb1-14" title="14">        <span class="co"># ip_hash：同一 IP 的请求始终路由到同一服务器（解决 session 会话保持）</span></a>
<a class="sourceLine" id="cb1-15" title="15">        <span class="co"># least_conn：优先将请求分配给当前连接数最少的服务器（适合长连接服务）</span></a>
<a class="sourceLine" id="cb1-16" title="16"></a>
<a class="sourceLine" id="cb1-17" title="17">        <span class="ex">server</span> backend1.example.com weight=3 max_fails=3 fail_timeout=30s slow_start=60s<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-18" title="18">        <span class="ex">server</span> backend2.example.com weight=2 max_fails=3 fail_timeout=30s slow_start=60s<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-19" title="19">        <span class="ex">server</span> backup.example.com backup<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-20" title="20">        </a>
<a class="sourceLine" id="cb1-21" title="21">        <span class="co">#服务器参数：</span></a>
<a class="sourceLine" id="cb1-22" title="22"></a>
<a class="sourceLine" id="cb1-23" title="23">        <span class="co"># weight=3：权重值，影响被选中概率（backend1承担约 3/5 流量）</span></a>
<a class="sourceLine" id="cb1-24" title="24">        <span class="co"># max_fails=3：连续 3 次失败后标记为不可用</span></a>
<a class="sourceLine" id="cb1-25" title="25">        <span class="co"># fail_timeout=30s：不可用状态持续 30 秒</span></a>
<a class="sourceLine" id="cb1-26" title="26">        <span class="co"># slow_start=60s：故障恢复后 60 秒内逐渐增加负载（避免流量冲击）</span></a>
<a class="sourceLine" id="cb1-27" title="27"></a>
<a class="sourceLine" id="cb1-28" title="28"></a>
<a class="sourceLine" id="cb1-29" title="29">        <span class="co"># 健康检查</span></a>
<a class="sourceLine" id="cb1-30" title="30">        <span class="ex">check</span> interval=5000 rise=2 fall=3 timeout=1000 type=http<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-31" title="31">        <span class="ex">check_http_send</span> <span class="st">&quot;GET /healthcheck HTTP/1.1\r\nHost: backend\r\nAuthorization: Basic dXNlcjpwYXNz\r\n\r\n&quot;</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-32" title="32">        <span class="ex">check_http_expect_alive</span> http_2xx http_3xx<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-33" title="33">        </a>
<a class="sourceLine" id="cb1-34" title="34">        <span class="co"># 健康检查机制：</span></a>
<a class="sourceLine" id="cb1-35" title="35"></a>
<a class="sourceLine" id="cb1-36" title="36">        <span class="co"># interval=5000：每 5 秒检查一次</span></a>
<a class="sourceLine" id="cb1-37" title="37">        <span class="co"># rise=2：连续 2 次成功视为健康</span></a>
<a class="sourceLine" id="cb1-38" title="38">        <span class="co"># fall=3：连续 3 次失败视为不健康</span></a>
<a class="sourceLine" id="cb1-39" title="39">        <span class="co"># timeout=1000：检查请求超时时间 1 秒</span></a>
<a class="sourceLine" id="cb1-40" title="40">        <span class="co"># type=http：使用 HTTP 协议检查</span></a>
<a class="sourceLine" id="cb1-41" title="41">        <span class="co"># check_http_send：发送带 Basic 认证的 GET 请求（用户名:password = user:pass） 也可以不用认证 </span></a>
<a class="sourceLine" id="cb1-42" title="42">        <span class="co"># check_http_expect_alive：仅 2xx/3xx 状态码视为健康</span></a>
<a class="sourceLine" id="cb1-43" title="43"></a>
<a class="sourceLine" id="cb1-44" title="44"><span class="co"># 后端服务验证</span></a>
<a class="sourceLine" id="cb1-45" title="45"><span class="co"># @app.route(&#39;/healthcheck&#39;)</span></a>
<a class="sourceLine" id="cb1-46" title="46"><span class="co"># def healthcheck():</span></a>
<a class="sourceLine" id="cb1-47" title="47"><span class="co">#     auth = request.authorization</span></a>
<a class="sourceLine" id="cb1-48" title="48"><span class="co">#     if not auth or not check_auth(auth.username, auth.password):</span></a>
<a class="sourceLine" id="cb1-49" title="49"><span class="co">#         return &quot;Unauthorized&quot;, 401</span></a>
<a class="sourceLine" id="cb1-50" title="50"></a>
<a class="sourceLine" id="cb1-51" title="51">    }</a>
<a class="sourceLine" id="cb1-52" title="52">    </a>
<a class="sourceLine" id="cb1-53" title="53">    <span class="ex">server</span> {</a>
<a class="sourceLine" id="cb1-54" title="54">        <span class="ex">listen</span> 443 ssl<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-55" title="55">        <span class="ex">server_name</span> example.com<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-56" title="56">        </a>
<a class="sourceLine" id="cb1-57" title="57">        <span class="co"># 安全头</span></a>
<a class="sourceLine" id="cb1-58" title="58">        <span class="ex">add_header</span> X-Content-Type-Options nosniff<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-59" title="59">        <span class="ex">add_header</span> X-Frame-Options DENY<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-60" title="60">        <span class="ex">add_header</span> Content-Security-Policy <span class="st">&quot;default-src &#39;self&#39;&quot;</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-61" title="61"></a>
<a class="sourceLine" id="cb1-62" title="62">        <span class="co"># 安全响应头：</span></a>
<a class="sourceLine" id="cb1-63" title="63"></a>
<a class="sourceLine" id="cb1-64" title="64">        <span class="co"># X-Content-Type-Options: nosniff：防止浏览器 MIME 类型嗅探攻击</span></a>
<a class="sourceLine" id="cb1-65" title="65">        <span class="co"># X-Frame-Options: DENY：禁止页面被 iframe 嵌入</span></a>
<a class="sourceLine" id="cb1-66" title="66">        <span class="co"># Content-Security-Policy: default-src &#39;self&#39;：仅允许加载同源资源</span></a>
<a class="sourceLine" id="cb1-67" title="67"></a>
<a class="sourceLine" id="cb1-68" title="68">        <span class="ex">location</span> / {</a>
<a class="sourceLine" id="cb1-69" title="69">            <span class="ex">proxy_pass</span> http://backend_servers<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-70" title="70">            <span class="ex">proxy_set_header</span> Host <span class="va">$host</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-71" title="71">            <span class="ex">proxy_set_header</span> X-Real-IP <span class="va">$remote_addr</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-72" title="72">            <span class="ex">proxy_set_header</span> X-Forwarded-For <span class="va">$proxy_add_x_forwarded_for</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-73" title="73">            <span class="ex">proxy_set_header</span> X-Forwarded-Proto <span class="va">$scheme</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb1-74" title="74">            </a>
<a class="sourceLine" id="cb1-75" title="75">        <span class="co"># 代理转发设置：</span></a>
<a class="sourceLine" id="cb1-76" title="76"></a>
<a class="sourceLine" id="cb1-77" title="77">        <span class="co"># proxy_pass：转发请求到backend_servers负载均衡组</span></a>
<a class="sourceLine" id="cb1-78" title="78">        <span class="co"># proxy_set_header：</span></a>
<a class="sourceLine" id="cb1-79" title="79">        <span class="co">#   Host $host：保留原始请求的域名</span></a>
<a class="sourceLine" id="cb1-80" title="80">        <span class="co">#   X-Real-IP $remote_addr：传递客户端真实 IP</span></a>
<a class="sourceLine" id="cb1-81" title="81">        <span class="co">#   X-Forwarded-For：追加代理服务器信息（格式：client, proxy1, proxy2）</span></a>
<a class="sourceLine" id="cb1-82" title="82">        <span class="co">#   X-Forwarded-Proto：传递原始请求协议（http/https）</span></a>
<a class="sourceLine" id="cb1-83" title="83"></a>
<a class="sourceLine" id="cb1-84" title="84"></a>
<a class="sourceLine" id="cb1-85" title="85"></a>
<a class="sourceLine" id="cb1-86" title="86">            <span class="co"># 失败重试</span></a>
<a class="sourceLine" id="cb1-87" title="87">            <span class="ex">proxy_next_upstream</span> error timeout http_500 http_502 http_503 http_504<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-88" title="88">            <span class="ex">proxy_next_upstream_tries</span> 3<span class="kw">;</span></a>
<a class="sourceLine" id="cb1-89" title="89"></a>
<a class="sourceLine" id="cb1-90" title="90"></a>
<a class="sourceLine" id="cb1-91" title="91">            <span class="co"># proxy_next_upstream：遇到指定错误时自动转发到下一台服务器</span></a>
<a class="sourceLine" id="cb1-92" title="92">            <span class="co"># proxy_next_upstream_tries：每个请求最多尝试 3 次</span></a>
<a class="sourceLine" id="cb1-93" title="93"></a>
<a class="sourceLine" id="cb1-94" title="94">        }</a>
<a class="sourceLine" id="cb1-95" title="95">        </a>
<a class="sourceLine" id="cb1-96" title="96">        <span class="co"># 内部健康检查端点（仅允许内网访问）</span></a>
<a class="sourceLine" id="cb1-97" title="97">        <span class="ex">location</span> = /internal_health {</a>
<a class="sourceLine" id="cb1-98" title="98">            <span class="ex">allow</span> 10.0.0.0/8<span class="kw">;</span>           <span class="co"># 允许10.0.0.0/8网段访问</span></a>
<a class="sourceLine" id="cb1-99" title="99">            <span class="ex">deny</span> all<span class="kw">;</span>                   <span class="co"># 拒绝其他所有访问</span></a>
<a class="sourceLine" id="cb1-100" title="100">            <span class="ex">stub_status</span><span class="kw">;</span>                <span class="co"># 输出Nginx状态信息</span></a>
<a class="sourceLine" id="cb1-101" title="101">        }</a>
<a class="sourceLine" id="cb1-102" title="102">    }</a>
<a class="sourceLine" id="cb1-103" title="103">}</a></code></pre></div>
<h3 id="二自定义日志格式处理及分析">二、自定义日志格式处理及分析</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb2-1" title="1"><span class="ex">http</span> {</a>
<a class="sourceLine" id="cb2-2" title="2">    <span class="co"># 生成唯一请求 ID（用于分布式追踪）</span></a>
<a class="sourceLine" id="cb2-3" title="3">    <span class="ex">load_module</span> modules/ngx_http_uuid_module.so<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-4" title="4">    <span class="ex">uuid_gen</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb2-5" title="5">    <span class="ex">uuid_gen_format</span> <span class="st">&quot;xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx&quot;</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb2-6" title="6">    </a>
<a class="sourceLine" id="cb2-7" title="7"></a>
<a class="sourceLine" id="cb2-8" title="8">    <span class="co"># 功能：生成符合 RFC 4122 标准的 UUID v4，作为请求唯一标识</span></a>
<a class="sourceLine" id="cb2-9" title="9">    <span class="co"># 关键点：</span></a>
<a class="sourceLine" id="cb2-10" title="10">    <span class="co">#     uuid_gen_format 中的 4 和 y 是 UUID v4 的固定格式要求</span></a>
<a class="sourceLine" id="cb2-11" title="11">    <span class="co">#     $request_id 变量用于在后续配置中引用生成的 UUID</span></a>
<a class="sourceLine" id="cb2-12" title="12">    <span class="co"># 用途：分布式系统中追踪请求链路（如从 Nginx 到多个微服务）</span></a>
<a class="sourceLine" id="cb2-13" title="13"></a>
<a class="sourceLine" id="cb2-14" title="14"></a>
<a class="sourceLine" id="cb2-15" title="15">    <span class="co"># 高性能 JSON 日志格式（适合 ELK/Vector 等工具解析）</span></a>
<a class="sourceLine" id="cb2-16" title="16">    <span class="ex">log_format</span> json_prod escape=json <span class="st">&#39;{</span></a>
<a class="sourceLine" id="cb2-17" title="17"><span class="st">        &quot;ts&quot;: &quot;$time_iso8601&quot;,</span></a>
<a class="sourceLine" id="cb2-18" title="18"><span class="st">        &quot;ip&quot;: &quot;$remote_addr&quot;,</span></a>
<a class="sourceLine" id="cb2-19" title="19"><span class="st">        &quot;xff&quot;: &quot;$http_x_forwarded_for&quot;,</span></a>
<a class="sourceLine" id="cb2-20" title="20"><span class="st">        &quot;req_id&quot;: &quot;$request_id&quot;,</span></a>
<a class="sourceLine" id="cb2-21" title="21"><span class="st">        &quot;method&quot;: &quot;$request_method&quot;,</span></a>
<a class="sourceLine" id="cb2-22" title="22"><span class="st">        &quot;path&quot;: &quot;$request_uri&quot;,</span></a>
<a class="sourceLine" id="cb2-23" title="23"><span class="st">        &quot;proto&quot;: &quot;$server_protocol&quot;,</span></a>
<a class="sourceLine" id="cb2-24" title="24"><span class="st">        &quot;status&quot;: $status,</span></a>
<a class="sourceLine" id="cb2-25" title="25"><span class="st">        &quot;size&quot;: $body_bytes_sent,</span></a>
<a class="sourceLine" id="cb2-26" title="26"><span class="st">        &quot;rt&quot;: $request_time,</span></a>
<a class="sourceLine" id="cb2-27" title="27"><span class="st">        &quot;upstream_rt&quot;: &quot;$upstream_response_time&quot;,</span></a>
<a class="sourceLine" id="cb2-28" title="28"><span class="st">        &quot;upstream_addr&quot;: &quot;$upstream_addr&quot;,</span></a>
<a class="sourceLine" id="cb2-29" title="29"><span class="st">        &quot;ua&quot;: &quot;$http_user_agent&quot;,</span></a>
<a class="sourceLine" id="cb2-30" title="30"><span class="st">        &quot;referer&quot;: &quot;$http_referer&quot;</span></a>
<a class="sourceLine" id="cb2-31" title="31"><span class="st">    }&#39;</span><span class="kw">;</span></a>
<a class="sourceLine" id="cb2-32" title="32">    </a>
<a class="sourceLine" id="cb2-33" title="33">    <span class="co"># &quot;ts&quot;: &quot;2023-10-15T14:30:45+08:00&quot;,      // ISO 8601 时间戳</span></a>
<a class="sourceLine" id="cb2-34" title="34">    <span class="co"># &quot;ip&quot;: &quot;192.168.1.100&quot;,                   // 客户端 IP</span></a>
<a class="sourceLine" id="cb2-35" title="35">    <span class="co"># &quot;xff&quot;: &quot;103.21.244.0&quot;,                   // 真实客户端 IP（通过代理传递）</span></a>
<a class="sourceLine" id="cb2-36" title="36">    <span class="co"># &quot;method&quot;: &quot;GET&quot;,                         // HTTP 请求方法</span></a>
<a class="sourceLine" id="cb2-37" title="37">    <span class="co"># &quot;req_id&quot;: &quot;f47ac10b-58cc-4372-a567-0e02b2c3d479&quot;,  // 请求唯一标识</span></a>
<a class="sourceLine" id="cb2-38" title="38">    <span class="co"># &quot;path&quot;: &quot;/api/users?page=1&quot;,             // 请求路径</span></a>
<a class="sourceLine" id="cb2-39" title="39">    <span class="co"># &quot;proto&quot;: &quot;HTTP/1.1&quot;,                     // HTTP 协议版本</span></a>
<a class="sourceLine" id="cb2-40" title="40">    <span class="co"># &quot;status&quot;: 200,                           // 响应状态码</span></a>
<a class="sourceLine" id="cb2-41" title="41">    <span class="co"># &quot;size&quot;: 1234,                            // 响应体大小（字节）</span></a>
<a class="sourceLine" id="cb2-42" title="42">    <span class="co"># &quot;rt&quot;: 0.235,                             // 请求总处理时间（秒）</span></a>
<a class="sourceLine" id="cb2-43" title="43">    <span class="co"># &quot;upstream_rt&quot;: &quot;0.210&quot;,                  // 上游服务器响应时间</span></a>
<a class="sourceLine" id="cb2-44" title="44">    <span class="co"># &quot;upstream_addr&quot;: &quot;10.0.0.1:8080&quot;,        // 上游服务器地址</span></a>
<a class="sourceLine" id="cb2-45" title="45">    <span class="co"># &quot;ua&quot;: &quot;Mozilla/5.0 (Windows NT 10.0)&quot;,   // 客户端 User-Agent</span></a>
<a class="sourceLine" id="cb2-46" title="46">    <span class="co"># &quot;referer&quot;: &quot;https://example.com/search&quot;  // 来源页面</span></a>
<a class="sourceLine" id="cb2-47" title="47"></a>
<a class="sourceLine" id="cb2-48" title="48"></a>
<a class="sourceLine" id="cb2-49" title="49">    <span class="co"># 简洁文本日志格式（适合快速排查问题）</span></a>
<a class="sourceLine" id="cb2-50" title="50">    <span class="ex">log_format</span> concise <span class="st">&#39;[$time_local] $remote_addr $status &#39;</span></a>
<a class="sourceLine" id="cb2-51" title="51">                       <span class="st">&#39;&quot;$request_method $request_uri&quot; &#39;</span></a>
<a class="sourceLine" id="cb2-52" title="52">                       <span class="st">&#39;rt=$request_time uct=&quot;$upstream_connect_time&quot; &#39;</span></a>
<a class="sourceLine" id="cb2-53" title="53">                       <span class="st">&#39;urt=&quot;$upstream_response_time&quot; req_id=&quot;$request_id&quot;&#39;</span>;</a>
<a class="sourceLine" id="cb2-54" title="54">    </a>
<a class="sourceLine" id="cb2-55" title="55">    <span class="co"># [15/Oct/2023:14:30:45 +0800] 192.168.1.100 200 &quot;GET /api/users?page=1&quot; rt=0.235 uct=&quot;0.012&quot; urt=&quot;0.210&quot; req_id=&quot;f47ac10b-58cc-4372-a567-0e02b2c3d479&quot;</span></a>
<a class="sourceLine" id="cb2-56" title="56"></a>
<a class="sourceLine" id="cb2-57" title="57">    <span class="ex">server</span> {</a>
<a class="sourceLine" id="cb2-58" title="58">        <span class="ex">listen</span> 443 ssl http2<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-59" title="59">        <span class="ex">server_name</span> example.com<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-60" title="60">        </a>
<a class="sourceLine" id="cb2-61" title="61">        <span class="co"># 主日志：JSON 格式，用于长期存储和分析</span></a>
<a class="sourceLine" id="cb2-62" title="62">        <span class="ex">access_log</span> /var/log/nginx/access.json json_prod buffer=32k flush=5s<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-63" title="63">        </a>
<a class="sourceLine" id="cb2-64" title="64">        <span class="co"># 性能优化：</span></a>
<a class="sourceLine" id="cb2-65" title="65">        <span class="co"># buffer=32k：32KB 缓冲区，减少磁盘 I/O 次数</span></a>
<a class="sourceLine" id="cb2-66" title="66">        <span class="co"># flush=5s：每 5 秒强制刷新一次，平衡实时性与性能</span></a>
<a class="sourceLine" id="cb2-67" title="67"></a>
<a class="sourceLine" id="cb2-68" title="68">        </a>
<a class="sourceLine" id="cb2-69" title="69">        <span class="co"># 错误日志：包含更详细的错误信息</span></a>
<a class="sourceLine" id="cb2-70" title="70">        <span class="ex">error_log</span> /var/log/nginx/error.log notice<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-71" title="71">        </a>
<a class="sourceLine" id="cb2-72" title="72">        <span class="co"># 静态资源路径：使用简洁日志减少磁盘 I/O</span></a>
<a class="sourceLine" id="cb2-73" title="73">        <span class="ex">location</span> ~* \.(js<span class="kw">|</span><span class="ex">css</span><span class="kw">|</span><span class="ex">png</span><span class="kw">|</span><span class="ex">jpg</span><span class="kw">|</span><span class="ex">gif</span><span class="kw">|</span><span class="ex">ico</span><span class="kw">|</span><span class="ex">woff</span><span class="kw">|</span><span class="ex">woff2</span><span class="kw">|</span><span class="ex">svg</span>)$ <span class="kw">{</span></a>
<a class="sourceLine" id="cb2-74" title="74">            <span class="ex">access_log</span> /var/log/nginx/static.log concise<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-75" title="75">            <span class="ex">expires</span> 7d<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-76" title="76">        <span class="kw">}</span></a>
<a class="sourceLine" id="cb2-77" title="77">        </a>
<a class="sourceLine" id="cb2-78" title="78">        <span class="co"># 使用简洁日志格式减少冗余信息</span></a>
<a class="sourceLine" id="cb2-79" title="79">        <span class="co"># 静态资源请求量大但业务价值低，单独存储便于分析</span></a>
<a class="sourceLine" id="cb2-80" title="80">        <span class="co"># expires 7d：浏览器缓存 7 天，减少重复请求</span></a>
<a class="sourceLine" id="cb2-81" title="81"></a>
<a class="sourceLine" id="cb2-82" title="82"></a>
<a class="sourceLine" id="cb2-83" title="83"></a>
<a class="sourceLine" id="cb2-84" title="84">        <span class="co"># 健康检查路径：关闭日志以减少噪声</span></a>
<a class="sourceLine" id="cb2-85" title="85">        <span class="ex">location</span> = /healthcheck {</a>
<a class="sourceLine" id="cb2-86" title="86">            <span class="ex">access_log</span> off<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-87" title="87">            <span class="bu">return</span> 200<span class="kw">;</span></a>
<a class="sourceLine" id="cb2-88" title="88">        }</a>
<a class="sourceLine" id="cb2-89" title="89">        </a>
<a class="sourceLine" id="cb2-90" title="90">        <span class="co"># 其他配置...</span></a>
<a class="sourceLine" id="cb2-91" title="91">    }</a>
<a class="sourceLine" id="cb2-92" title="92">}</a></code></pre></div>
</body>
</html>
